package com.youlai.boot.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtils {
    public ExcelUtils() {
    }

    public static String getExcelWriteName(String name) {
        return name + "-" + LocalDateTimeUtil.now().format(DatePattern.PURE_DATETIME_FORMATTER) + ".xlsx";
    }

    public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ((ExcelWriterBuilder)((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream(), head).autoCloseStream(false).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())).registerConverter(new LocalDateConverter())).sheet(sheetName).doWrite(data);
    }


    public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data, SheetWriteHandler sheetWriteHandler) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream(), head).autoCloseStream(false).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())).registerConverter(new LocalDateConverter())).registerWriteHandler(sheetWriteHandler)).sheet(sheetName).doWrite(data);
    }

    public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data, AbstractMergeStrategy strategy) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream(), head).autoCloseStream(false).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())).registerConverter(new LocalDateConverter())).registerWriteHandler(strategy)).sheet(sheetName).doWrite(data);
    }

    public static <T> void writeTemplate(HttpServletResponse response, String filename, String templatePath, Object data) throws IOException {
        defaultHeader(response, filename);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setWrapped(true);
        InputStream resourceAsStream = ExcelUtils.class.getClassLoader().getResourceAsStream(templatePath);
        ((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream()).registerWriteHandler(new HorizontalCellStyleStrategy((WriteCellStyle)null, contentWriteCellStyle))).withTemplate(resourceAsStream).sheet().doFill(data);
    }

    public static void defaultHeader(HttpServletResponse response, String filename) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    }

    public static String getCellName(int index) {
        StringBuilder name;
        for(name = new StringBuilder(); index >= 0; index = index / 26 - 1) {
            int i = index % 26;
            name.append((char)(i + 65));
        }

        return name.reverse().toString();
    }
}
